<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>MongoDB Index | Introduction</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../home/part2/9.MongoDBDump.html" />
    
    
    <link rel="prev" href="../../home/part2/7.MongoDB查询条件.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="3.8"
        data-chapter-title="MongoDB Index"
        data-filepath="home/part2/8.MongoDBIndex.md"
        data-basepath="../.."
        data-revision="Sun Oct 21 2018 20:49:12 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Introduction
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="home/NoSQL简介.html">
            
                
                    <a href="../../home/NoSQL简介.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        NoSQL介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="home/part1/Redis.html">
            
                
                    <a href="../../home/part1/Redis.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        Redis
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="home/part1/1.常用命令.html">
            
                
                    <a href="../../home/part1/1.常用命令.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        常用命令
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="home/part1/2.Redis的数据类型.html">
            
                
                    <a href="../../home/part1/2.Redis的数据类型.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        Redis的数据类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="home/part1/3.Redis高级.html">
            
                
                    <a href="../../home/part1/3.Redis高级.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        Redis高级
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="home/part1/4.python操作redis.html">
            
                
                    <a href="../../home/part1/4.python操作redis.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        python操作redis
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="home/part2/MongoDB.html">
            
                
                    <a href="../../home/part2/MongoDB.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        MongoDB
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="home/part2/1.MongoDB基础操作.html">
            
                
                    <a href="../../home/part2/1.MongoDB基础操作.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        MongoDB基础操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="home/part2/2.MongoDBDBS.html">
            
                
                    <a href="../../home/part2/2.MongoDBDBS.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        MongoDB DBS
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="home/part2/3.MongoDBCreate.html">
            
                
                    <a href="../../home/part2/3.MongoDBCreate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        MongoDB Create
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="home/part2/4.MongoDBUpdate.html">
            
                
                    <a href="../../home/part2/4.MongoDBUpdate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        MongoDB Update
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="home/part2/5.MongoDBDelete.html">
            
                
                    <a href="../../home/part2/5.MongoDBDelete.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        MongoDB Delete
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="home/part2/6.MongoDBRetrieve.html">
            
                
                    <a href="../../home/part2/6.MongoDBRetrieve.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        MongoDB Retrieve
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="home/part2/7.MongoDB查询条件.html">
            
                
                    <a href="../../home/part2/7.MongoDB查询条件.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        MongoDB 查询条件
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.8" data-path="home/part2/8.MongoDBIndex.html">
            
                
                    <a href="../../home/part2/8.MongoDBIndex.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        MongoDB Index
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="home/part2/9.MongoDBDump.html">
            
                
                    <a href="../../home/part2/9.MongoDBDump.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        MongoDB Dump
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="home/part2/10.Python操作MongoDB.html">
            
                
                    <a href="../../home/part2/10.Python操作MongoDB.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                        Python操作MongoDB
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Introduction</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="mongodb-&#x7D22;&#x5F15;">MongoDB &#x7D22;&#x5F15;</h1>
<p>&#x7D22;&#x5F15;&#x901A;&#x5E38;&#x80FD;&#x591F;&#x6781;&#x5927;&#x7684;&#x63D0;&#x9AD8;&#x67E5;&#x8BE2;&#x7684;&#x6548;&#x7387;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x7D22;&#x5F15;&#xFF0C;MongoDB&#x5728;&#x8BFB;&#x53D6;&#x6570;&#x636E;&#x65F6;&#x5FC5;&#x987B;&#x626B;&#x63CF;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x6BCF;&#x4E2A;&#x6587;&#x4EF6;&#x5E76;&#x9009;&#x53D6;&#x90A3;&#x4E9B;&#x7B26;&#x5408;&#x67E5;&#x8BE2;&#x6761;&#x4EF6;&#x7684;&#x8BB0;&#x5F55;&#x3002;</p>
<p>&#x8FD9;&#x79CD;&#x626B;&#x63CF;&#x5168;&#x96C6;&#x5408;&#x7684;&#x67E5;&#x8BE2;&#x6548;&#x7387;&#x662F;&#x975E;&#x5E38;&#x4F4E;&#x7684;&#xFF0C;&#x7279;&#x522B;&#x5728;&#x5904;&#x7406;&#x5927;&#x91CF;&#x7684;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x67E5;&#x8BE2;&#x53EF;&#x4EE5;&#x8981;&#x82B1;&#x8D39;&#x51E0;&#x5341;&#x79D2;&#x751A;&#x81F3;&#x51E0;&#x5206;&#x949F;&#xFF0C;&#x8FD9;&#x5BF9;&#x7F51;&#x7AD9;&#x7684;&#x6027;&#x80FD;&#x662F;&#x975E;&#x5E38;&#x81F4;&#x547D;&#x7684;&#x3002;</p>
<p>&#x7D22;&#x5F15;&#x662F;&#x7279;&#x6B8A;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF0C;&#x7D22;&#x5F15;&#x5B58;&#x50A8;&#x5728;&#x4E00;&#x4E2A;&#x6613;&#x4E8E;&#x904D;&#x5386;&#x8BFB;&#x53D6;&#x7684;&#x6570;&#x636E;&#x96C6;&#x5408;&#x4E2D;&#xFF0C;&#x7D22;&#x5F15;&#x662F;&#x5BF9;&#x6570;&#x636E;&#x5E93;&#x8868;&#x4E2D;&#x4E00;&#x5217;&#x6216;&#x591A;&#x5217;&#x7684;&#x503C;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x7684;&#x4E00;&#x79CD;&#x7ED3;&#x6784;</p>
<hr>
<h2 id="ensureindex-&#x65B9;&#x6CD5;">ensureIndex() &#x65B9;&#x6CD5;</h2>
<p>MongoDB&#x4F7F;&#x7528; ensureIndex() &#x65B9;&#x6CD5;&#x6765;&#x521B;&#x5EFA;&#x7D22;&#x5F15;&#x3002;</p>
<h3 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;</h3>
<p>ensureIndex()&#x65B9;&#x6CD5;&#x57FA;&#x672C;&#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-sql">&gt;db.COLLECTION_NAME.ensureIndex({KEY:1})
</code></pre>
<h3 id="&#x5B9E;&#x4F8B;">&#x5B9E;&#x4F8B;</h3>
<pre><code class="lang-sql">&#x6DFB;&#x52A0;&#x666E;&#x901A;&#x7D22;&#x5F15;
db.col.ensureIndex({&quot;title&quot;:1})

&#x67E5;&#x8BE2;&#x7D22;&#x5F15;
db.col.getIndexes()

&#x5220;&#x9664;&#x7D22;&#x5F15;
db.col.dropIndex({title:1})

&#x6DFB;&#x52A0;&#x552F;&#x4E00;&#x7D22;&#x5F15;  &#x6CE8;&#x610F; &#x5B57;&#x6BB5;&#x4FDD;&#x6301;&#x552F;&#x4E00; &#x5426;&#x5219;&#x62A5;&#x9519;,&#x5220;&#x9664;&#x540E;&#x53EF;&#x4EE5;&#x6DFB;&#x52A0;
db.lis.ensureIndex({name:1},{unique:true})

&#x67E5;&#x8BE2;&#x5206;&#x6790;
db.lis.find({&quot;name&quot;:&apos;a&apos;}).explain()
</code></pre>
<p>ensureIndex() &#x65B9;&#x6CD5;&#x4E2D;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;&#x4F7F;&#x7528;&#x591A;&#x4E2A;&#x5B57;&#x6BB5;&#x521B;&#x5EFA;&#x7D22;&#x5F15;&#xFF08;&#x5173;&#x7CFB;&#x578B;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x79F0;&#x4F5C;&#x590D;&#x5408;&#x7D22;&#x5F15;&#xFF09;&#x3002;</p>
<pre><code class="lang-sql">&gt;db.col.ensureIndex({&quot;title&quot;:1,&quot;description&quot;:-1})
&gt;
</code></pre>
<p>ensureIndex() &#x63A5;&#x6536;&#x53EF;&#x9009;&#x53C2;&#x6570;&#xFF0C;&#x53EF;&#x9009;&#x53C2;&#x6570;&#x5217;&#x8868;&#x5982;&#x4E0B;&#xFF1A;</p>
<table>
<thead>
<tr>
<th>Parameter</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>background</td>
<td>Boolean</td>
<td>&#x5EFA;&#x7D22;&#x5F15;&#x8FC7;&#x7A0B;&#x4F1A;&#x963B;&#x585E;&#x5176;&#x5B83;&#x6570;&#x636E;&#x5E93;&#x64CD;&#x4F5C;&#xFF0C;background&#x53EF;&#x6307;&#x5B9A;&#x4EE5;&#x540E;&#x53F0;&#x65B9;&#x5F0F;&#x521B;&#x5EFA;&#x7D22;&#x5F15;&#xFF0C;&#x5373;&#x589E;&#x52A0; &quot;background&quot; &#x53EF;&#x9009;&#x53C2;&#x6570;&#x3002; &quot;background&quot; &#x9ED8;&#x8BA4;&#x503C;&#x4E3A;false&#x3002;</td>
</tr>
<tr>
<td>unique</td>
<td>Boolean</td>
<td>&#x5EFA;&#x7ACB;&#x7684;&#x7D22;&#x5F15;&#x662F;&#x5426;&#x552F;&#x4E00;&#x3002;&#x6307;&#x5B9A;&#x4E3A;true&#x521B;&#x5EFA;&#x552F;&#x4E00;&#x7D22;&#x5F15;&#x3002;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A;false.</td>
</tr>
<tr>
<td>name</td>
<td>string</td>
<td>&#x7D22;&#x5F15;&#x7684;&#x540D;&#x79F0;&#x3002;&#x5982;&#x679C;&#x672A;&#x6307;&#x5B9A;&#xFF0C;MongoDB&#x7684;&#x901A;&#x8FC7;&#x8FDE;&#x63A5;&#x7D22;&#x5F15;&#x7684;&#x5B57;&#x6BB5;&#x540D;&#x548C;&#x6392;&#x5E8F;&#x987A;&#x5E8F;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x7D22;&#x5F15;&#x540D;&#x79F0;&#x3002;</td>
</tr>
</tbody>
</table>
<h3 id="&#x5B9E;&#x4F8B;">&#x5B9E;&#x4F8B;</h3>
<p>&#x5728;&#x540E;&#x53F0;&#x521B;&#x5EFA;&#x7D22;&#x5F15;&#xFF1A;</p>
<pre><code class="lang-sql">db.values.ensureIndex({open: 1, close: 1}, {background: true})
</code></pre>
<p>&#x901A;&#x8FC7;&#x5728;&#x521B;&#x5EFA;&#x7D22;&#x5F15;&#x65F6;&#x52A0;background:true &#x7684;&#x9009;&#x9879;&#xFF0C;&#x8BA9;&#x521B;&#x5EFA;&#x5DE5;&#x4F5C;&#x5728;&#x540E;&#x53F0;&#x6267;&#x884C;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../home/part2/7.MongoDB查询条件.html" class="navigation navigation-prev " aria-label="Previous page: MongoDB 查询条件"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../home/part2/9.MongoDBDump.html" class="navigation navigation-next " aria-label="Next page: MongoDB Dump"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
